//
//  HR_Touch_PreviewViewController.m
//  3DTouch
//
//  Created by duanhuifen on 2017/7/22.
//  Copyright © 2017年 huifen. All rights reserved.
//

#import "HR_Touch_PreviewViewController.h"
//#import "HR_Touch_CustomController.h
#import "HR_Touch_ImageViewController.h"

@interface HR_Touch_PreviewViewController ()
@property (nonatomic,assign) NSInteger actionIndex;
@end

@implementation HR_Touch_PreviewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.previewType == HR_Touch_PreviewType_Image) {
        HR_Touch_ImageViewController * imageVC = [[HR_Touch_ImageViewController alloc] init];
        imageVC.picName = self.imageName;
        [self.view addSubview:imageVC.view];
        [self addChildViewController:imageVC];
        self.preViewActionArr = @[@"下载"];
    }else{
        [self loadViewController];
    }
}

////重置本控制器的大小
//-(CGSize)preferredContentSize{
//    
//    if (self.popoverPresentationController != nil) {
//                CGSize tempSize ;
//                tempSize.height = self.view.frame.size.height/2;
//                tempSize.width  = 50;
//                tempSize  = [self.view sizeThatFits:tempSize];
//                return tempSize;
//        
//    }else{
//        return CGSizeMake(50, 50);
//    }
//    
//}


//加载控制器
- (void)loadViewController{
    if (self.customControllerName) {
        UIViewController * customVC = [NSClassFromString(self.customControllerName) new] ;
        [self.view addSubview:customVC.view];
        [self addChildViewController:customVC];
    }
}

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {

    return  [self creatPreviewActionWithTitle:self.preViewActionArr];
}

- (NSMutableArray *)creatPreviewActionWithTitle:(NSArray *)titleArr{
    __weak typeof(self)weakSelf = self;
    NSMutableArray * preViewActionArr = [NSMutableArray array];
    
    for (NSString * titleStr in titleArr) {
        UIPreviewAction *action = [UIPreviewAction actionWithTitle:titleStr
                                                             style:UIPreviewActionStyleDefault
                                                           handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
                                                                                                                               [weakSelf previewActionWithTitle:titleStr];
                                                           }];
        [preViewActionArr addObject:action];
    }
    return preViewActionArr;
}

- (void)previewActionWithTitle:(NSString *)title{
    [self.preViewActionArr enumerateObjectsUsingBlock:^(NSString *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isEqualToString:title]) {
            self.actionIndex = idx;
            [self previewActionClick];
        }
    }];
}

//previewAction的点击
- (void)previewActionClick{
    NSLog(@"%ld",(long)self.actionIndex);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
